layui.use(['table', 'admin', 'ax', 'func','upload','form','laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var upload = layui.upload;
    var form = layui.form;
    var laydate = layui.laydate;

    /**
     * 合同管理管理
     */
    var BtyContract = {
        tableId: "btyContractTable"
    };

    /**
     * 导入Excel
     */
    var loadIndex;
    upload.render({
        elem: '#btnExcel'
        , url: '/btyContract/import'
        , accept: "file"
        , exts: 'xls|xlsx|xlsm|xlt|xltx|xltm'
        , before: function () {
            loadIndex = layer.load('正在导入...', 2, {shade: [0.15, '#ccc']});
            this.data = {'zdrq': $('#zdrq').val()};//关键代码
        }
        , done: function (res) {
            if (res.state == 0) {
                Feng.error("导入失败!" + res.errmsg);
            }
            if (res == true) {
                Feng.success("导入成功!");
                window.location.reload();
            }
            if (res == false) {
                Feng.error("导入失败!");
                window.location.reload();
            }
            layer.close(loadIndex);
        }
    });

    /**
     * 初始化表格的列
     */
    BtyContract.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'primaryArea', sort: true, title: '一级区域'},
            {field: 'secondaryArea', sort: true, title: '二级区域'},
            {field: 'cwm', sort: true, title: '财务码'},
            {field: 'dm', sort: true, title: '店名'},
            {field: 'state', sort: true, title: '1已收回 2未收回 3复印件 4电子版 5无 6合同不全'},
            {field: 'contractRemarks', sort: true, title: '合同备注'},
            {field: 'firstParty', sort: true, title: '甲方'},
            {field: 'secondParty', sort: true, title: '乙方'},
            {field: 'developer', sort: true, title: '开发人员'},
            {field: 'openDate', sort: true, title: '开业时间'},
            {field: 'area', sort: true, title: '面积'},
            {field: 'startTime', sort: true, title: '起租时间'},
            {field: 'endTime', sort: true, title: '到期时间'},
            {field: 'firstMonthlyRent', sort: true, title: '首期月租金'},
            {field: 'firstMonthlyPropertyFee', sort: true, title: '首期月物业费'},
            {field: 'rentPaymentCycle', sort: true, title: '租金缴纳周期1月付 2俩月付 3季付 4半年付 5年付'},
            {field: 'billingMethod', sort: true, title: '计费方式1每日每平 2每月每平 3固定计费'},
            {field: 'expenseName', sort: true, title: '费用名称'},
            {field: 'dailyAverageAmount', sort: true, title: '每日每平金额'},
            {field: 'monthlyAverageAmount', sort: true, title: '每月每平金额'},
            {field: 'monthlyAmount', sort: true, title: '月金额'},
            {field: 'detailDate', sort: true, title: '递增明细日期'},
            {field: 'totalAmount', sort: true, title: '总金额'},
            {field: 'detailRemark', sort: true, title: '递增明细备注'},
            {field: 'rentDate', sort: true, title: '租金日期'},
            {field: 'rentBonus', sort: true, title: '租金抽成'},
            {field: 'otherExpensesName', sort: true, title: '其他费用名称'},
            {field: 'otherExpensesAmount', sort: true, title: '其他费用金额'},
            {field: 'bondName', sort: true, title: '保证金名称'},
            {field: 'bondAmount', sort: true, title: '保证金金额'},
            {field: 'remark', sort: true, title: '备注'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    BtyContract.search = function () {
        var queryData = {};

        queryData['primaryArea'] = $('#primaryArea').val();
        queryData['secondaryArea'] = $('#secondaryArea').val();
        queryData['dm'] = $('#dm').val();

        table.reload(BtyContract.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加对话框
     */
    BtyContract.openAddDlg = function () {
        func.open({
            title: '添加合同管理',
            content: Feng.ctxPath + '/btyContract/add',
            tableId: BtyContract.tableId
        });
    };

     /**
      * 点击编辑
      *
      * @param data 点击按钮时候的行数据
      */
      BtyContract.openEditDlg = function (data) {
          func.open({
              title: '修改合同管理',
              content: Feng.ctxPath + '/btyContract/edit?id=' + data.id,
              tableId: BtyContract.tableId
          });
      };


    /**
     * 导出excel按钮
     *
     * primaryArea一级区域
     secondaryArea二级区域
     cwm财务码
     state合同是否收回
     developer开发人员
     endTime到期时间
     rentPaymentCycle缴纳周期
     rentBonus抽成租金

     */
    BtyContract.exportExcel = function () {

        // var primaryArea = $('#primaryArea').val();
        // var secondaryArea = $('#secondaryArea').val();
        // var dm = $('#dm').val();
        // var cwm = $('#cwm').val();
        // var state = $('#state').val();
        //
        // var developer = $('#developer').val();
        // var state = $('#state').val();
        // var state = $('#state').val();
        // var state = $('#state').val();

        var url = Feng.ctxPath + "/btyContract/export";
        location.href = url;

        // var checkRows = table.checkStatus(BtyContract.tableId);
        // if (checkRows.data.length === 0) {
        //     Feng.error("请选择要导出的数据");
        // } else {
        //     table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        // }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    BtyContract.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/btyContract/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(BtyContract.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + BtyContract.tableId,
        url: Feng.ctxPath + '/btyContract/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: BtyContract.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        BtyContract.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {

    BtyContract.openAddDlg();

    });

    // 导出excel
    $('#btnExp').click(function () {
        BtyContract.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + BtyContract.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            BtyContract.openEditDlg(data);
        } else if (layEvent === 'delete') {
            BtyContract.onDeleteItem(data);
        }
    });
});
